from django.db import models
from datetime import datetime
from organization.models import *
# Create your models here.
class Course(models.Model):
    course_org = models.ForeignKey(CourseOrg,verbose_name='课程机构',null=True,blank=True)
    teacher = models.ForeignKey(Teacher, verbose_name='课程老师', null=True, blank=True)
    name = models.CharField(max_length=100,verbose_name='课程名')
    desc = models.CharField(max_length=500,verbose_name='课程描述')
    is_banner = models.BooleanField(default=False,verbose_name="是否轮播图")
    detail = models.TextField(verbose_name='课程详情')
    degree = models.CharField(choices=(('cj','初级'),('zj','中级'),('gj','高级')),max_length=10,verbose_name='难度')
    learn_times = models.IntegerField(default=0,verbose_name="学习时长（分钟）")
    students = models.IntegerField(default=0,verbose_name='学习人数')
    favor_nums = models.IntegerField(default=0,verbose_name='收藏人数')
    image = models.ImageField(upload_to='courses/%Y/%m',verbose_name='封面图',max_length=200)
    click_num = models.IntegerField(default=0,verbose_name='点击数')
    category = models.CharField(default='后端开发',max_length=100,verbose_name='课程类别')
    tag = models.CharField(default='',verbose_name='课程标签',max_length=30)
    target = models.CharField(default='',verbose_name='课程目标',max_length=300)
    add_time = models.DateTimeField(default=datetime.now,verbose_name='添加时间')
    class Meta:
        verbose_name = '课程'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
    #获取课程章节数
    def zj_nums(self):
        return self.lesson_set.all().count()

    zj_nums.short_description = '章节数'
    #获取用户课程信息
    def learn_users(self):
        return self.usercourse_set.all()[:5]
    #获取课程章节信息
    def course_lessons(self):
        return self.lesson_set.all()

class BannerCourse(Course):
    class Meta:
        verbose_name = "轮播课程"
        verbose_name_plural = verbose_name
        proxy = True

class Lesson(models.Model):
    course = models.ForeignKey(Course,verbose_name='课程')
    name = models.CharField(max_length=100,verbose_name='章节名')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    class Meta:
        verbose_name = '章节'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name
    #获取lesson的课程视频信息
    def lesson_videos(self):
        return self.video_set.all()

class Video(models.Model):
    lesson = models.ForeignKey(Lesson,verbose_name='章节')
    name = models.CharField(max_length=100,verbose_name='视频名')
    learn_times = models.IntegerField(default=0, verbose_name="学习时长（分钟）")
    url = models.CharField(max_length=500,verbose_name='访问地址',default='')
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    class Meta:
        verbose_name = '课程视频'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class CourseResorce(models.Model):
    course = models.ForeignKey(Course, verbose_name='课程')
    name = models.CharField(max_length=100, verbose_name='名称')
    download = models.FileField(upload_to='course/resorce/%Y%m',verbose_name='资源文件',max_length=100)
    add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
    class Meta:
        verbose_name = '课程资源'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name